import Vue from 'vue'

import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
  {
    path: '/free',
    name: 'free',
  
    component: () => import(/* webpackChunkName: "free" */ '../views/FreeView.vue')
    
  },
  {
    path: '/goods',
    name: 'goods',
  
    component: () => import(/* webpackChunkName: "goods" */ '../views/GoodsView.vue')
  
  },
  {
    path: '/order',
    name: 'order',
  
    component: () => import(/* webpackChunkName: "order" */ '../views/OrderView.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect:"/user/cart",
    component: () => import(/* webpackChunkName: "user" */ '../views/UserView.vue'),
    children:[
      {
        path: 'cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "cart" */ '../components/user/Cart.vue')
      },
    ],
  },
  {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "details" */ '../views/DetailsView.vue')
  },
  {
    path: '*',

    component: () => import(/* webpackChunkName: "notfount" */ '../views/NotFount.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};

//路由守卫，导航守卫
// router.beforeEach((to,from,next)=>{
//   //什么时候执行这个函数？每一次路由跳转之前都执行这个函数

//   //to 要去往的那个路有的对象 to.path 就拿到药品去的这个路径
//   //from 从哪里触发的这个路由对象 from.path 就拿到触发的这个路径
  
//   //需求：没有登陆不能进入个人中心
//   //实现思路：先判断是不是去个人中心，如果是，再判断有没有登录
//   if(to.path === "/user"){
//     let token = localStorage.getItem("x-auth-token");
//     if(token){
//       next()
//     }else{

//       router.push(from.path)
//     }
//     return
//   }

//   //from
//   next()
// })

export default router
